###########################################################
# $Id: xrsh_common.sh 4506 2012-12-23 04:15:49Z xrdk $
# brief	    : common shell script
# author    : xrdk
# version   : V1.0, 2004-03-16
#
# CopyRight (C) 2004-2011 xrdk.
# All rights reserved.
###########################################################

if test "$xrsh_common" != "defined"; then

xrsh_common=defined

xrsh_comment=

if [ $xrsh_comment ]; then
  comment block samples
fi

#=====================================================================
export xrsh_cd="cd"
export xrsh_ls="ls -l"

export xrsh_cp="cp"
export xrsh_cp_f="cp -f"
export xrsh_cp_dir="cp -f -R"
export xrsh_rm_f="rm -f"
export xrsh_mv_f="mv -f"
export xrsh_rmdir="rmdir"
export xrsh_mkdir="mkdir -p"

export xrsh_xdir="test -d"
export xrsh_xfile="test -e"
export xrsh_chmod="chmod 777"
export xrsh_ln="ln -sf"
#=====================================================================
#Get current full path
#xrtmp=`xrsh_getcurpath`
function xrsh_getcurpath()
{
  local _xrsh_tmp=`echo $0 | grep "^/"`
  if test "${_xrsh_tmp}"; then
    dirname $0
  else
    dirname `pwd`/$0
  fi
}

#Get parent dir. Args: 1 path
function xrsh_getparentdir()
{
	if [ "$1" = "" ]; then return; fi
	local _xrsh_tmp=`pwd`
	dirname "$1"
	cd "$_xrsh_tmp"
}

#Get exe path. Args: 1 exe name; 2 alt. path. Use "which exename" find, if ok return else return alt.(arg 2)
#xrtmp=`xrsh_getexepath "gcc" "/usr/bin/gcc"`
function xrsh_getexepath()
{
	if [ "$1" = "" ]; then return; fi
  local _xrsh_tmp=`which $1`
  _xrsh_tmp=`echo $_xrsh_tmp | grep "/$1"`
  if test "${_xrsh_tmp}"; then
    echo $_xrsh_tmp
  else
    echo "$2"
  fi
}

#Get exe dir. Args: 1 exe name; 2 alt. path.
#xrtmp=`xrsh_getexedir "gcc" "/usr/bin/gcc"`
#xrtmp=/usr/bin
function xrsh_getexedir()
{
	local _xrsh_tmp=`xrsh_getexepath $1 $2`
	if [ "$_xrsh_tmp" = "" ]; then return; fi
	xrsh_getparentdir $_xrsh_tmp
}

#Get exe parent dir. Args: 1 exe name; 2 alt. path.
#xrtmp=`xrsh_getexedir_parent "gcc" "/usr/bin/gcc"`
#xrtmp=/usr
function xrsh_getexedir_parent()
{
	local _xrsh_tmp=`xrsh_getexedir $1 $2`
	if [ "$_xrsh_tmp" = "" ]; then return; fi
	xrsh_getparentdir $_xrsh_tmp
}
#=====================================================================
#Convert arch. to universe name. Args: 1 arch. name
#x86: i386, i486, ...
#x64: x86_64, amd64, AMD64
function xrsh_cvt_archname()
{
	case $1 in
	  i[3456789]86|x86|i86pc|x86pc)
	    echo x86
	    return
	    ;;
	  x86_64|amd64|AMD64)
	    echo x64
	    return
	    ;;
	  *)
	    ;;
	esac
	echo $1
}

#Check arch. is 64 bit. Args: 1 arch. name
function xrsh_isarch64()
{
	case $1 in
	  x64|x86_64|amd64|AMD64|powerpc|ia64)
	    echo yes
	    return
	    ;;
	  *)
	    ;;
	esac
	echo no
}

#Convert os name. Args: 1 os name
function xrsh_cvt_osname()
{
	local _xrsh_tmp=$1
	
	case $_xrsh_tmp in
	  Linux)	    _xrsh_tmp=lnx	    ;;
	  SunOS)	    _xrsh_tmp=sun	    ;;
	  AIX)	      _xrsh_tmp=aix	    ;;
	  HP-UX)	    _xrsh_tmp=hpux    ;;
	  Darwin)	    _xrsh_tmp=osx	    ;;
	  FreeBSD)	  _xrsh_tmp=freebsd ;;
	  QNX)	      _xrsh_tmp=qnx	    ;;
	  *)	    ;;
	esac
	echo $_xrsh_tmp
}
#=====================================================================
#Check item is in array. Args: 1 array; 2 item
function xrsh_hasitem()
{
	local _xrsh_tmp
  local _xrsh_array=`echo "$1"`
  echo $1
  for _xrsh_tmp in ${_xrsh_array[*]}; do
    if test "$2" = "$_xrsh_tmp"; then
      echo yes
      return
    fi
  done
  echo no
}

#Get item index in array. Args: 1 array; 2 item. Return: index, start 0; -1 no found
function xrsh_itemidx()
{
  local _xrsh_tmp
  local _xrsh_cnt=0
  local _xrsh_array=`echo "$1"`
  for _xrsh_tmp in ${_xrsh_array[*]}; do
    if test "$2" = "$_xrsh_tmp"; then
      echo $_xrsh_cnt
      return
    fi
    _xrsh_cnt=$(( $_xrsh_cnt + 1 ))
  done
  echo "-1"
}

function xrsh_itemcvt_inc()
{
	local _xrsh_tmp
	local _xrsh_out=
	local _xrsh_array=`echo "$1"`
 	for _xrsh_tmp in ${_xrsh_array[*]}; do
    _xrsh_out="$_xrsh_out -I$_xrsh_tmp"
  done
  
  echo "$_xrsh_out"
}

function xrsh_itemcvt_lib()
{
	local _xrsh_tmp
	local _xrsh_out=
	local _xrsh_array=`echo "$1"`
 	for _xrsh_tmp in ${_xrsh_array[*]}; do
    _xrsh_out="$_xrsh_out -L$_xrsh_tmp"
  done
  
  echo "$_xrsh_out"
}

#Callback fn for array items, aka. foreach
#Args: 1 array; 2 fn; 3 user arg(option)
#fn args: 1 item index(start 0); 2 item; 3 user arg
#code
# function xrsh_fn()
# {
#   echo index $1, item $2, usr $3
# }
# xrsh_array=(i1 i2 i3)
# xrsh_tmp=`echo ${xrsh_array[*]}`
# xrsh_foreach "$xrsh_tmp" xrsh_fn
#endcode
function xrsh_foreach()
{
  local _xrsh_tmp
  local _xrsh_cnt=0
  local _xrsh_array=`echo "$1"`
  for _xrsh_tmp in ${_xrsh_array[*]}; do
    $2 $_xrsh_cnt $_xrsh_tmp $3
    _xrsh_cnt=$(( $_xrsh_cnt + 1 ))
  done
}
#=====================================================================
#Convert lower
function xrsh_tolower()
{
	echo $1 | tr '[A-Z]' '[a-z]'
}

#Convert upper
function xrsh_toupper()
{
	echo $1 | tr '[a-z]' '[A-Z]'
}

#Compare two num. Args: 1 num1; 2 num2
#return: 0 num1=num2; 1 num1>num2; -1 num1<num2;
#note: lt le ge gt eq ne
function xrsh_cmpnum()
{
	#[ `expr $1 \>= $2` -eq 1 ]
	if [ $1 -eq $2 ]; then
		echo 0
	elif [ $1 -gt $2 ]; then
		echo 1
	else
		echo -1
	fi
}

#if arg 1 valid then return arg 1 else return arg 2
function xrsh_altval()
{
  if test "$1" = ""; then
    echo "$2"
  else
    echo "$1"
  fi
}

#Get spec. place of num. Args: 1 num; 2 place(option)
#code
# get hundred's place of num 423, retrun 4
#  xrsh_getnumplace 423 100
# get lowest place of num 423, return 3
#  xrsh_getnumplace 423
#endcode 
function xrsh_getnumplace()
{
	local _xrsh_tmp
	
	if [ "$2" = "" ]; then
		_xrsh_tmp=`expr $1 \% 10`
	else
		_xrsh_tmp=`expr $2 \* 10`
		_xrsh_tmp=`expr $1 \% $(($_xrsh_tmp+0))`
		_xrsh_tmp=`expr $(($_xrsh_tmp+0)) \/ $2`
	fi
	echo $_xrsh_tmp
}
#=====================================================================
#Get dot string spec. pos. num. Args: 1 str; 2 pos[1,3]
#note: format n.n.n
#code
# get 1.2.3 pos 3, return 3
#  xrsh_getdotnum "1.2.3" 3
# get 1.2.3 pos 1, return 1
#  xrsh_getdotnum "1.2.3"
#endcode
function xrsh_getdotnum()
{
	if test "$2" = "3"; then
		echo $1 | sed -n 's/[0-9]\{1,10\}\.[0-9]\{1,10\}\.\([0-9]\{1,10\}\) *.*/\1/p'
	elif test "$2" = "2"; then
		echo $1 | sed -n 's/[0-9]\{1,10\}\.\([0-9]\{1,10\}\) *.*/\1/p'
	else
		echo $1 | sed -n 's/\([0-9]\{1,10\}\).[0-9]\{1,10\} *.*/\1/p'
	fi
}

#Get dot num sub string of any string
#note: format n.n.n
#code
# get "xyz1.2.3", return 1.2.3 
#  xrsh_getdotnumstr "xyz1.2.3"
#endcode
function xrsh_getdotnumstr()
{
	#echo $1 | sed -n 's/.*\([0-9]\{1,10\}\.[0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'
	echo $1 | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'
}

#Get dot num sub string of any string
#note: format n.n
#see: xrsh_getdotnumstr
function xrsh_getdot2numstr()
{
	#echo $1 | sed -n 's/.*\([0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'
	echo $1 | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'
}
#=====================================================================
fi #if test "$xrsh_common" != "defined"; then
